
import java.io.*;

public class StoreRecordManager {

    public static void main(String[] args) {
        // 模拟进货记录
        recordPurchase("2022-01-15", "商品A", 100, 50.0);
        recordPurchase("2022-01-16", "商品B", 150, 60.0);

        // 模拟交易记录
        recordTransaction("2022-01-17", "商品A", 50, 55.0);
        recordTransaction("2022-01-18", "商品B", 100, 65.0);

        // 读取并打印进货记录
        readAndPrintPurchaseRecords();

        // 读取并打印交易记录
        readAndPrintTransactionRecords();
    }

    public static void recordPurchase(String date, String itemName, int quantity, double price) {
        try (DataOutputStream out = new DataOutputStream(new FileOutputStream("purchase_records.dat", true))) {
            out.writeUTF(date);
            out.writeUTF(itemName);
            out.writeInt(quantity);
            out.writeDouble(price);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void recordTransaction(String date, String itemName, int quantity, double price) {
        try (DataOutputStream out = new DataOutputStream(new FileOutputStream("transaction_records.dat", true))) {
            out.writeUTF(date);
            out.writeUTF(itemName);
            out.writeInt(quantity);
            out.writeDouble(price);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readAndPrintPurchaseRecords() {
        try (DataInputStream in = new DataInputStream(new FileInputStream("purchase_records.dat"))) {
            while (in.available() > 0) {
                String date = in.readUTF();
                String itemName = in.readUTF();
                int quantity = in.readInt();
                double price = in.readDouble();
                System.out.println("进货记录 - 日期: " + date + ", 商品: " + itemName + ", 数量: " + quantity + ", 价格: " + price);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readAndPrintTransactionRecords() {
        try (DataInputStream in = new DataInputStream(new FileInputStream("transaction_records.dat"))) {
            while (in.available() > 0) {
                String date = in.readUTF();
                String itemName = in.readUTF();
                int quantity = in.readInt();
                double price = in.readDouble();
                System.out.println("交易记录 - 日期: " + date + ", 商品: " + itemName + ", 数量: " + quantity + ", 价格: " + price);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
